package org.cos.verifier.process;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.cos.verifier.utility.IOWrapper;

public class ProfileManager {
	private Hashtable profiles;
	private Hashtable selected_prof;
	private String prof_file;
	private String selected_prof_name;
	
	static ProfileManager instance;
	public static ProfileManager get_inst() {
		if(instance == null)
			instance = new ProfileManager();
		return instance;
	} 
	
	public ProfileManager() {
		profiles = new Hashtable();
		selected_prof = null;
	}
	
	public void set_prof_file(String prof_file) {
		this.prof_file = prof_file;
	}
	
	
	
	public void load() {
		byte[] raw =null;
		try {
			raw = IOWrapper.get_inst().read_file(prof_file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		ByteArrayInputStream bin = new ByteArrayInputStream(raw);
		DataInputStream din = new DataInputStream(bin);
		int prof_cnt = 0;
		try {
			prof_cnt=din.readInt();
		} catch (IOException e1) {
			return;
		}
		for (int k = 0; k < prof_cnt; k+=1){
			try {
				String prof_name = din.readUTF();
				int rule_cnt = din.readInt();
				Hashtable prof = new Hashtable();
				for (int i = 0; i < rule_cnt; i+=1){
					String rule_name = din.readUTF();
					int rule_checked = din.readInt();
					prof.put(rule_name, new Integer(rule_checked));
				}
				profiles.put(prof_name, prof);
			} catch (EOFException e){
				break;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			din.close();
			bin.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void dump(){
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		DataOutputStream dout = new DataOutputStream(bout);
		try {
			dout.writeInt(profiles.size());
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		for (Enumeration enumx = profiles.keys(); enumx.hasMoreElements();){
			String prof_name = (String) enumx.nextElement();
			Hashtable prof = (Hashtable) profiles.get(prof_name);

			try {
				dout.writeUTF(prof_name);
				dout.writeInt(prof.size());
				
				for (Enumeration enumy = prof.keys();enumy.hasMoreElements();){
					String rule_name = (String)enumy.nextElement();
					int rule_chked = ((Integer)prof.get(rule_name)).intValue();

					dout.writeUTF(rule_name);
					dout.writeInt(rule_chked);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} 
		}
		byte[] raw = bout.toByteArray();
		try {
			IOWrapper.get_inst().write_file(prof_file, raw);
			dout.close();
			bout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Hashtable select(String name){
		if (!profiles.containsKey(name))
			profiles.put(name, new Hashtable());
		selected_prof = (Hashtable) profiles.get(name);
		selected_prof_name = name;

		return selected_prof;
	}
	
	public Hashtable crr_profile(){
		return selected_prof;
	}
	public String crr_profile_name(){
		return selected_prof_name;
	}
	
	public Vector get_all_names(){
		Vector ret = new Vector();
		ret.add("default");
		return ret;
	}
	
	public void sync(Hashtable rules){
		for (Enumeration ex = selected_prof.keys(); ex.hasMoreElements();){
			String name = (String) ex.nextElement();
			if (! rules.containsKey(name)){
				selected_prof.remove(name);
			}
		}
		for (Enumeration ex = rules.keys(); ex.hasMoreElements();){
			String r_name = (String) ex.nextElement();
			if (! selected_prof.containsKey(r_name)){
				selected_prof.put(r_name, new Integer(0));
			}
		}

	}
}
 